thin.imports('/public/home/css/global.project.css');

thin.imports('/public/home/configure/conf.activity.js');
thin.imports('/public/home/configure/conf.project.js');

thin.use(function() {
	actid = tool.GetQueryString('actid');

	// 路由
	checkLocation();

	$('#rolist, #aclist, #mylist').on('click', '#btn-des', function() {
		actid = $(this).attr('data-id');
		checkLocation('details');
	});

	$('[layout-mark=tab-item]').off('click').on('click', function(e) {
		var e = e || window.event;
		e.stopPropagation();
		var that = $(this),
			itor = that.attr('data-itor'),
			type = that.attr('data-type');
		__getdata_actvolist['type'] = type;
		that.addClass('weui-bar__item--on').siblings('a').removeClass('weui-bar__item--on');
		$(itor).addClass('weui-tab__bd-item--active').siblings('div').removeClass('weui-tab__bd-item--active');
		getVolist();
		return false;
	});

	$('#btn-sing-pro').on('click', function() {
		var _id = $('[name=prolists]:checked').val();
		if(_id) {
			$.showLoading();
			__getdata_actup['project_id'] = _id;
			factory.request('_activity_up');
		} else {
			$.toptip('请选择项目', 'error');
		}
	});
});

function __complete_actvolist(res, unkn) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.list.length > 0) {
			var ele = createVolist(res.data);
			$('#' + unkn).append(ele);
		} else {
			$('#' + unkn).parent().addClass('nothing-bg');
		}
	} else {
		$('#' + unkn).parent().addClass('nothing-bg');
	}
}

function __complete_actmelist(res) {
	$.hideLoading();
	if(res.status == 200) {
		if(res.data.length > 0) {
			var ele = createMyVolist(res.data);
			$('#mylist').append(ele);
		} else {
			$('#mylist').parent().addClass('nothing-bg');
		}
	} else {
		$('#mylist').parent().addClass('nothing-bg');
	}
}

function getVolist() {
	if(__getdata_actvolist['type'] == 1 && !jude_tablist_o) {
		$.showLoading();
		factory.request('_activity_volist', 'rolist');
		jude_tablist_o = true;
	}
	if(__getdata_actvolist['type'] == 2 && !jude_tablist_t) {
		$.showLoading();
		factory.request('_activity_volist', 'aclist');
		jude_tablist_t = true;
	}
}

function createVolist(data) {
	var ele = '';
	for(var i in data.list) {
		var list = data.list[i];
		ele += '<a href="javascript:void(0);" class="weui-panel weui-panel_access bg-ffffff m-t-1x position-relative" style="display: block;" id="btn-des" data-id="' + list.id + '">';
		ele += '<div class="weui-media-box weui-media-box_appmsg weui-line-reset">';
		ele += '<div class="m-r-2x">';
		ele += '<img class="aty-thumb" src="' + list.cover + '" alt="">';
		ele += '</div>';
		ele += '<div class="weui-media-box__bd">';
		ele += '<p class="fs16r ellipsis-area2">';
		ele += '<font class="pro-status bg-ff5722 m-r-1x">HOT</font>';
		ele += list.title;
		ele += '</p>';
		ele += '<p class="fs12r c-888 p-t-1x"><i class="icon-address"></i> ' + list.localtion + '  ' + list.address + '</p>';
		ele += '<p class="fs12r c-888"><i class="icon-time"></i> 开始时间：' + tool.formatDate('yy-mm-dd hh:ii', list.start_time) + '</p>';
		ele += '<p class="fs12r c-888"><i class="icon-time"></i> 结束时间：' + tool.formatDate('yy-mm-dd hh:ii', list.end_time) + '</p>';
		ele += '</div>';
		ele += '</div>';
		ele += '<div class="weui-panel__ft">';
		ele += '<p class="fs14r c-888 p-x-2x p-y-1x">主办方：' + list.organizers + '<font class="float-right">' + list.sign_up_count + '人报名参加</font></p>';
		ele += '</div>';
		if(list.is_sign_up == 1) {
			ele += '<img src="/public/home/images/sign_up.png" class="position-absolute position-top position-right" />';
		}
		ele += '</a>';
	}
	return ele;
}

function createMyVolist(data) {
	var ele = '';
	for(var i in data) {
		var list = data[i].activity;
		ele += '<a href="javascript:void(0);" class="weui-panel weui-panel_access bg-ffffff m-t-1x position-relative" style="display: block;" id="btn-des" data-id="' + list.id + '">';
		ele += '<div class="weui-media-box weui-media-box_appmsg">';
		ele += '<div class="m-r-2x">';
		ele += '<img class="aty-thumb" src="' + list.cover + '" alt="">';
		ele += '</div>';
		ele += '<div class="weui-media-box__bd">';
		ele += '<p class="fs16r ellipsis-area2">';
		ele += '<font class="pro-status bg-ff5722 m-r-1x">HOT</font>';
		ele += list.title;
		ele += '</p>';
		ele += '<p class="fs12r c-888 p-t-1x"><i class="icon-address"></i> ' + list.localtion + '  ' + list.address + '</p>';
		ele += '<p class="fs12r c-888"><i class="icon-time"></i> 开始时间：' + tool.formatDate('yy-mm-dd hh:ii', list.start_time) + '</p>';
		ele += '<p class="fs12r c-888"><i class="icon-time"></i> 结束时间：' + tool.formatDate('yy-mm-dd hh:ii', list.end_time) + '</p>';
		ele += '</div>';
		ele += '</div>';
		ele += '<img src="/public/home/images/sign_up.png" class="position-absolute position-top position-right" />';
		ele += '</a>';
	}
	return ele;
}

function __complete_actdetail(res) {
	console.dir(res);
	$.hideLoading();
	if(res.status == 200) {
		detaildata[res.data.id] = res.data;
		__getdata_actvolist['type'] = res.data.type;
		binddetail(res.data);
	} else {

	}
}

function binddetail(data) {
	$('#bind-de-cover').attr('src', data.cover);
	$('#bind-de-title').text(data.title);
	var st = tool.formatDate('yy-mm-dd hh:ii', data.start_time);
	var et = tool.formatDate('yy-mm-dd hh:ii', data.end_time);
	$('#bind-de-datetime').html('开始时间：' + st + '<br />结束时间：' + et);
	$('#bind-de-position').text(data.localtion + '  ' + data.address);
	$('#bind-de-organizers').text(data.organizers);
	$('#bind-de-introduction').text(data.introduction);
	$('#bind-de-sign_up_count').text(data.sign_up_count);
	if(data.sign_up_count > 0) {
		var ele = '';
		for(var i in data.sign_top10_list) {
			var list = data.sign_top10_list[i];
			ele += '<img src="' + list.headimgurl + '" class="avatars" />';
		}
		$('#bind-de-sith').html(ele);
	}
	if(data.is_sign_up == 0) {
		$('#btn-sing').text('立即报名').removeClass('weui-btn_warn').addClass('weui-btn_primary').off('click').on('click', deSingUp);
	}
	if(data.is_sign_up == 1) {
		$('#btn-sing').text('取消报名').removeClass('weui-btn_primary').addClass('weui-btn_warn').off('click').on('click', deSingOut);
	}
}

var __getdata_actup = {},
	__getdata_actout = {};

function deSingUp() {
	var type = __getdata_actvolist['type'];
	if(type == 1) {
		$.showLoading();
		__getdata_actup['activity_id'] = actid;
		factory.request('_activity_up');
	} else {
		__getdata_actup['activity_id'] = actid;
		$.showLoading();
		factory.request('_project_cxlist');
	}
}

function deSingOut() {
	$.showLoading();
	__getdata_actout['activity_id'] = actid;
	factory.request('_activity_out');
}

function __complete_actout(res) {
	$.hideLoading();
	if(res.status == 204) {
		$.toast("报名已取消", 960);
		setTimeout(function() {
			if(actid == tool.GetQueryString('actid')) window.location.reload();
			else window.location.href = '/mobile/chain/activity?actid=' + actid + '#details';
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_procxlist(res) {
	$.hideLoading();
	if(res.status == 200) {
		var ele = '';
		for(var i in res.data) {
			var data = res.data[i];
			ele += '<label class="weui-cell weui-check__label" for="x' + data.id + '">';
			ele += '<div class="weui-cell__bd">';
			ele += '<p>' + data.title + '</p>';
			ele += '</div>';
			ele += '<div class="weui-cell__ft">';
			ele += '<input type="radio" class="weui-check" name="prolists" id="x' + data.id + '" value="' + data.id + '">';
			ele += '<span class="weui-icon-checked"></span>';
			ele += '</div>';
			ele += '</label>';
		}
		$('#prolists-view').html(ele);
		$("#sel-pro-list").popup();
	}
}

function __complete_actup(res) {
	$.hideLoading();
	delete __getdata_actup['project_id'];
	if(res.status == 201) {
		$.toast("报名成功", 960);
		setTimeout(function() {
			if(actid == tool.GetQueryString('actid')) window.location.reload();
			else window.location.href = '/mobile/chain/activity?actid=' + actid + '#details';
		}, 800);
	} else {
		$.toptip(res.error, 'error');
	}
}

var __getdata_actvolist = {
		'type': 1
	},
	__getdata_actdetail = {},
	detaildata = {},
	actid = null;

var jude_tablist_y = false,
	jude_tablist_o = false,
	jude_tablist_t = false;

// 路由
function checkLocationCallback(hash) {
	if(hash == 'tablist') {
		getVolist();
	}

	if(hash == 'details') {
		if(actid) {
			if(!detaildata[actid]) {
				$.showLoading();
				__getdata_actdetail['id'] = actid;
				factory.request('_activity_get');
			} else {
				binddetail(detaildata[actid]);
			}
		} else {
			checkLocation('tablist');
		}
	}

	if(hash == 'myact') {
		if(!jude_tablist_y) {
			$.showLoading();
			factory.request('_activity_melist');
			jude_tablist_y = true;
		}
	}
}